package org.jackkang.elasticsearch.controller;

import java.util.Locale;

import javax.annotation.Resource;

import org.jackkang.elasticsearch.model.News;
import org.jackkang.elasticsearch.service.NewsService;
import org.jackkang.elasticsearch.util.NewsSyncService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * Handles requests for the application home page.
 */
@Controller
public class HomeController {
	@Resource
	private NewsService newsService;
	
	@Resource
	private NewsSyncService newsSyncService;
	
	private static final Logger logger = LoggerFactory
			.getLogger(HomeController.class);

	@Resource
	private ElasticsearchTemplate elasticsearchTemplate;

    /**
     * 
     * @Title: traditional
     * @Description: 
     * @param locale
     * @param model
     * @param query
     * @param page
     * @param size
     * @return String
     * @author  kangjie1209@126.com
     * @date 2016年11月13日 下午2:22:46
     */
	@RequestMapping(value = "/search", method = RequestMethod.GET)
	public String traditional(Locale locale, Model model,
			@RequestParam(value = "keyword", required = false) String keyword,
			@RequestParam(value = "page", required = false, defaultValue = "1") int page, @RequestParam(value = "size", required = false, defaultValue = "10") int size) {

		page -= 1;
		Pageable pageable = new PageRequest(page, size);
		Page<News> pageObj;
		/*if (StringUtils.isBlank(query)) {
			pageObj = newsRepository.findAll(pageable);
		} else {
			pageObj = newsRepository.findByContent(query, pageable);
		}*/
		
		/*model.addAttribute("total", pageObj.getTotalPages());
		model.addAttribute("books", pageObj.getContent());*/
		model.addAttribute("page", page + 1);
		
		return "traditional";
	}
}
